﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter
{
    public class BayerFilter : BaseImageFilter
    {
        /// <summary>
        /// 
        /// </summary>
        public BayerFilter()
        {
        }

        /// <summary>
        /// internal constructor used for cloning
        /// </summary>
        /// <param name="_bayerFilter"></param>
        internal BayerFilter(BayerFilter _bayerFilter)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap  Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bmp = _bitmap;
            // we are going to cheat and ignore the boundaries
            // dealing with the boundaries is not difficult,
            // but the code is long enough as it is
            for (int y = 1; y < bmp.Height - 1; y++)
            {
                for (int x = 1; x < bmp.Width - 1; x++)
                {
                   // //first we are going to recover the pixel neighborhood.
                   // //the mask at the end is simply to get rid of the alpha
                   // //channel
                   // //middlecenter is the pixel we are working with (x,y)
                   // int topleft = bmp[x - 1 + (y - 1) * bmp.Width] & 0xFFFFFF;
                   // int topcenter = bmp.Pixels[x + (y - 1) * bmp.Width] & 0xFFFFFF;
                   // int topright = bmp.Pixels[x + 1 + (y - 1) * bmp.Width] & 0xFFFFFF;
                   // int middleleft = bmp.Pixels[x - 1 + y * bmp.Width] & 0xFFFFFF;
                   // int middlecenter = bmp.Pixels[x + y * bmp.Width] & 0xFFFFFF;
                   // int middleright = bmp.Pixels[x + 1 + y * bmp.Width] & 0xFFFFFF;
                   // int bottomleft = bmp.Pixels[x - 1 + (y + 1) * bmp.Width] & 0xFFFFFF;
                   // int bottomcenter = bmp.Pixels[x + (y + 1) * bmp.Width] & 0xFFFFFF;
                   // int bottomright = bmp.Pixels[x + 1 + (y + 1) * bmp.Width] & 0xFFFFFF;

                   // int blue = 0;
                   // int red = 0;
                   // int green = 0;
 
                   // // if we are on an even row and an even column
                   // // like (2, 2) then we are on a green pixel
                   // // we need to average top center and bottom
                   // // center for red we need to average left
                   // // middle and right middle for blue

                   // if (y % 2 == 0 && x % 2 == 0)
                   //{
                   //     red = (((topcenter >> 16) + (bottomcenter >> 16)) / 2) << 16;
                   //     blue = (middleleft + middleright) / 2;
                   //     green = middlecenter;
                   // }

                   // // if we are on an even row and an odd column
                   // // like (2, 1) then we are on a blue pixel
                   // // red is an average of top left, top right,
                   // // bottom left, and bottom right
                   // // green is top center, bottom center,
                   // // left middle, and right middle
                   // else if (y % 2 == 0 && x % 2 == 1)
                   // {
                   //     red = (((topleft >> 16) +
                   //         (topright >> 16) +
                   //         (bottomleft >> 16) +
                   //         (bottomright >> 16)) / 4) << 16;
                
                   //     blue = middlecenter;

                   //     green = (((topcenter >> 8 ) +
                   //         (bottomcenter >> 8 ) +
                   //         (middleleft >> 8 ) +
                   //         (middleright >> 8 )) / 4) << 8;
                   // }

                   // // if we are on an odd row and an even column
                   // // like (1, 2) then we are on a red pixel
                   // // blue is an average of top left, top right,
                   // // bottom left, and bottom right
                   // // green is top center, bottom center,

                   // // left middle, and right middle

                   // else if (y % 2 == 1 && x % 2 == 0)

                   // {
                   //     red = middlecenter;
                   //     blue = (topleft +
                   //         topright +
                   //         bottomleft +
                   //         bottomright) / 4;

                   //     green = (((topcenter >> 8 ) +
                   //         (bottomcenter >> 8 ) +
                   //         (middleleft >> 8 ) +
                   //         (middleright >> 8 )) / 4) << 8;

                   // }


                   // // otherwise we are on an odd row and odd column
                   // // like (1,1). this is a green pixel
                   // // red left middle + right middle
                   // // blue is top center + bottom center
                   // else
                   // {
                   //     red = (((middleleft >> 16) +
                   //         (middleright >> 16)) / 2) << 16;

                   //     blue = (topcenter + bottomcenter) / 2;
                   //      green = middlecenter;
                   // }

                   // interpolatedBmp.Pixels[x + y * interpolatedBmp.PixelWidth] = (255 << 24) | red | green | blue;
                }
            }
                //return interpolatedBmp;
                return null;
            }

        /// <summary>
        /// Cloning
        /// </summary>
        /// <returns></returns>
        public override object  Clone()
        {
            return new BayerFilter(this);
        }
    }
}
